package com.example.social.me.project;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class Map_Activity extends MapActivity implements LocationListener{
	private double langitude;
	private double longitude;
	
	MapView map_view;
	MapController map_controller;
	Geocoder coder;
	String provider;
	LocationManager map_manager;
	private MyItemizedOverlay nearPeople_Overlay;
	private OverlayItem near_items[] = new OverlayItem[100];
	private Drawable us_Pic, 
						  fr_Pic,
						  fl_Pic;
	public static Context context;
		
			protected void onPause() {
			// TODO Auto-generated method stub
			super.onPause();
			}

			protected void onResume() {
			IntentFilter filter = new IntentFilter("lns.create.project.broadcast");
			BackServiceReceiver receiver = new BackServiceReceiver();
			registerReceiver(receiver,filter);
			
			langitude = receiver.get_langitude();
			longitude = receiver.get_Longitude();
			super.onResume();
			}

			@Override
			protected void onCreate(Bundle savedInstanceState) {
		
				super.onCreate(savedInstanceState);
				setContentView(R.layout.activity_map);
				
				context = getApplicationContext();
				
				map_view = (MapView)findViewById(R.id.MapView);
				//map_view.setClickable(false);
				map_view.setBuiltInZoomControls(true);
				
				map_controller = map_view.getController();
				map_controller.setZoom(16);
				map_controller.animateTo(new GeoPoint(37450877,126768494));
				
				nearPeople_Overlay = new MyItemizedOverlay(getResources().getDrawable(android.R.drawable.ic_delete));
				OverlayItem overlayItem = new OverlayItem(new GeoPoint(37450877,126768494), "Here1!",null);
				OverlayItem overlayItem2 = new OverlayItem( new GeoPoint((int)(37.536138 * 1E6),
						(int)(127.214813 * 1E6)),"here",null);
				nearPeople_Overlay.addOverlay(overlayItem2);
				nearPeople_Overlay.addOverlay(overlayItem);
				map_view.getOverlays().add(nearPeople_Overlay);
				checkProvider();
				
			}
			
			public double Calculate_distance()
			{
				return langitude;
				
			}
			public void checkProvider(){
				map_manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
				Criteria cta = new Criteria();
				cta.setAccuracy(Criteria.ACCURACY_FINE);
				cta.setPowerRequirement(Criteria.POWER_HIGH);
				cta.setAltitudeRequired(false);
				cta.setCostAllowed(false);
				provider = map_manager.getBestProvider(cta, true);
			}
			public void onLocationChanged(Location location) {
				GeoPoint gp = new GeoPoint((int)(location.getLatitude() * 1E6),
														(int)(location.getLongitude() * 1E6));
				map_controller.animateTo(gp);
				
			}
			public void onProviderDisabled(String arg0) {
				// TODO Auto-generated method stub
				
			}
			public void onProviderEnabled(String arg0) {
				// TODO Auto-generated method stub
				
			}
			public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
				// TODO Auto-generated method stub
				
			}
	
			@Override
			protected boolean isRouteDisplayed() {
				// TODO Auto-generated method stub
				return false;
			}

		
}

class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> myOverlays ;

    public MyItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
        myOverlays = new ArrayList<OverlayItem>();
        populate();
    }

    public void addOverlay(OverlayItem overlay){
        myOverlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return myOverlays.get(i);
    }

    // Removes overlay item i
    public void removeItem(int i){
        myOverlays.remove(i);
        populate();
    }

    // Returns present number of items in list
    @Override
    public int size() {
        return myOverlays.size();
    }


    public void addOverlayItem(OverlayItem overlayItem) {
        myOverlays.add(overlayItem);
        populate();
    }


    public void addOverlayItem(int lat, int lon, String title) {
        try {
            GeoPoint point = new GeoPoint(lat, lon);
            OverlayItem overlayItem = new OverlayItem(point, title, null);
            addOverlayItem(overlayItem);    
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    @Override
    protected boolean onTap(int index) {
        // TODO Auto-generated method stub
        String title = myOverlays.get(index).getTitle();
        Toast.makeText(Map_Activity.context, title, Toast.LENGTH_LONG).show();
        return super.onTap(index);
    }
}

class BackServiceReceiver extends BroadcastReceiver
{
	private double longitude;
	private double langitude;
	@Override
	public void onReceive(Context context, Intent intent) {
		String msg = intent.getStringExtra("MSG_READ");
		
	}
	
	public double get_Longitude()
	{
		return langitude;
		
	}
	
	public double get_langitude()
	{
		return langitude;
		
	}
	
}
